package org.jxht.service.impl;

import org.jxht.dao.StudentDao;
import org.jxht.dto.MajProDto;
import org.jxht.dto.StudentDto;
import org.jxht.service.MajProServie;
import org.jxht.service.StudentService;
import org.jxht.util.ConstantUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by huang on 2017/3/1 0001.
 */

@Service
public class StudentServiceImpl implements StudentService {

    @Resource
    StudentDao studentDao;

    @Resource
    MajProServie majProServie;

    /**
     * 添加学生用户
     * @param dto 学生用户实体
     */
    public void insertStudentService(StudentDto dto) {
        studentDao.insertStudentDao(dto);
    }

    /**
     * 根据requestId获取学生信息
     * @param requestId 学号、专业号、系号
     * @param requestWay 请求方式
     * @return StudentDto
     */
    public List queryStudentService(int requestId, String requestWay) {
        if("all".equals(requestWay)) {
            return studentDao.queryStudentDao(new StudentDto());
        }
        List<StudentDto> studentDtos = null;
        StudentDto dto = new StudentDto();
        if(ConstantUtils.STU_ID.equals(requestWay)){
            dto.setStuId(requestId);
        }else if(ConstantUtils.STU_COU_ID.equals(requestWay)){
            dto.setStuProId(Integer.toString(requestId));
        }else{
            MajProDto majProDto = new MajProDto();
            if(ConstantUtils.PRO_ID.equals(requestWay)){
                if(requestId<=4)
                    majProDto.setProId("0"+Integer.toString(requestId));
                majProDto.setProId(Integer.toString(requestId));
            }else {
                majProDto.setMajId(requestId);
            }
            List<MajProDto> majprolists = majProServie.queryMajProService(majProDto);
            if(majprolists.size() > 0){
                studentDtos = new ArrayList<StudentDto>();
                for(MajProDto list:majprolists) {
                    dto.setStuProId(list.getStuProId());
                    studentDtos.addAll(studentDao.queryStudentDao(dto));
                }
                return studentDtos;
            }
            return null;
        }
        return studentDao.queryStudentDao(dto);
    }

    public StudentDto queryStudentServiceSingle(StudentDto dto) {
        return (StudentDto) studentDao.queryStudentDao(dto).get(0);
    }
//     更新学生信息
    public void updataStudentService(StudentDto dto) {
        studentDao.updataStudentDao(dto);
    }
//     删除学生信息
    public void deleteStudentService(StudentDto dto) {
        studentDao.deleteStudentDao(dto);
    }
}
